<route lang="json5">
{
  style: {
    navigationBarTitleText: '意见反馈',
    // #ifdef MP-ALIPAY
    enablePullDownRefresh: false,
    'mp-alipay': {
      allowsBounceVertical: 'NO',
      transparentTitle: 'always',
      titlePenetrate: 'YES',
      defaultTitle: '',
    },
    // #endif
  },
}
</route>
<template>
  <view class="page">
    <view class="card">
      <view class="card-title">
        问题和意见
        <text class="card-must">*</text>
      </view>
      <view class="card-body">
        <textarea
          placeholder="请填写您的功能建议,感谢您的支持"
          maxlength="200"
          v-model="inputText"
        ></textarea>
      </view>
    </view>
    <view class="card">
      <view class="card-title">问题截图</view>
      <view class="card-body">
        <view class="card-imgs">
          <view class="card-imgs-item" v-for="(v, k) in imgs" :key="k">
            <image :src="v.src" />
            <view class="card-imgs-item-del" @tap="delImg(k)">
              <image src="@/static/global/del.png"></image>
            </view>
            <view class="card-imgs-item-mask" v-if="v.uploading">上传中</view>
          </view>
          <view class="card-imgs-upload" v-if="imgs.length < 5" @tap="upload">
            <image src="@/static/my/upload.png" />
            <text>上传凭证</text>
            <text>(最多5张)</text>
          </view>
        </view>
      </view>
    </view>
    <!-- <view class="card">
      <view class="card-title">联系方式</view>
      <view class="card-body">
        <input placeholder="留下联系方式,可能更快的解决问题~" />
      </view>
    </view> -->
    <view class="button" @tap="submit">提交</view>
  </view>
</template>

<script setup lang="ts">
import { getEnvBaseUrl } from '@/utils'
import { useUserStore } from '@/store/user'
import { feedbackApi } from '@/service/global'
import { SearchTypeEnum } from '@/enums/SearchTypeEnum'

const baseUrl = getEnvBaseUrl()

const inputText = ref<string>('')
const imgs = ref<any>([])

const upload = async () => {
  uni.chooseImage({
    success: async (chooseImageRes) => {
      const tempFilePaths = chooseImageRes.tempFilePaths
      imgs.value.push({ src: tempFilePaths[0] })
      console.log('tempFilePaths', tempFilePaths)
    },
  })
}

const delImg = async (index: number) => {
  imgs.value.splice(index, 1)
}

const submit = async () => {
  if (!inputText.value || inputText.value === '') {
    uni.showToast({
      title: '请填写内容',
      icon: 'error',
    })
    return
  }
  uni.showLoading({ title: '提交中...', mask: true })
  const feedbackRes = await feedbackApi(1, inputText.value)
  const feedbackId = feedbackRes.data.id
  for (const element of imgs.value) {
    const res = await uni.uploadFile({
      url: baseUrl + '/file/upload', // 仅为示例，非真实的接口地址
      filePath: element.src,
      name: 'file',
      formData: {
        type: SearchTypeEnum.FEEDBACK,
        param: JSON.stringify({ id: feedbackId }),
      },
    })
    console.log('upload avatar resp', res)
  }
  uni.hideLoading()
  uni.navigateBack()
  uni.showToast({
    title: '提交成功',
  })
}
</script>

<style lang="scss">
.page {
  width: 100vw;
  min-height: 100vh;
  background: #fafafa;

  .card {
    width: 710rpx;
    margin: 20rpx auto;

    .card-title {
      padding: 15rpx;
      font-size: 14px;
      color: #555;

      .card-must {
        color: red;
      }
    }

    .card-body {
      padding: 15rpx;
      background: #fff;
      border-radius: 12rpx;

      textarea {
        height: 300rpx;
        font-size: 14px;
      }

      input {
        height: 70rpx;
        font-size: 14px;
      }

      .card-imgs {
        display: flex;
        flex-wrap: wrap;

        .card-imgs-item {
          position: relative;
          display: flex;
          align-items: center;
          justify-content: center;
          width: 150rpx;
          height: 150rpx;
          margin: 10rpx;
          background: #444;
          border-radius: 12rpx;

          .card-imgs-item-mask {
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 150rpx;
            height: 150rpx;
            font-size: 14px;
            color: #fff;
            background: rgba(0, 0, 0, 0.5);
          }

          image {
            width: 60rpx;
            height: 60rpx;
            border-radius: 12rpx;
          }

          .card-imgs-item-del {
            position: absolute;
            top: -15rpx;
            right: -15rpx;
            z-index: 2;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 40rpx;
            height: 40rpx;
            background: #fff;
            border-radius: 50%;
            image {
              width: 40rpx;
              height: 40rpx;
            }
          }
        }

        .card-imgs-upload {
          box-sizing: border-box;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          width: 150rpx;
          height: 150rpx;
          margin: 10rpx;
          background: #ddd;
          border: 1px #777 dashed;
          border-radius: 12rpx;
          &:active {
            background: #ccc;
          }

          text {
            font-size: 12px;
            color: #777;
          }
        }
      }
    }
  }

  .button {
    width: 500rpx;
    height: 80rpx;
    margin: 20rpx auto;
    font-size: 16px;
    font-weight: bold;
    line-height: 80rpx;
    color: #fff;
    text-align: center;
    background-color: #55aaff;
    border-radius: 40rpx;
    &:active {
      background-color: #3d7bb8;
    }
  }
}
</style>
